package com.ruicar.afs.cloud.apply.pre.loan.mq.receiver;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.common.entity.ApplyCustBaseInfo;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOprRecord;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOrderInfo;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyCustBaseInfoService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOprRecordService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOrderInfoService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.BusinessStateInEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ResultBooleanEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.CallBackApplyResultDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.parameter.commom.enums.OrderOprType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>Description：</p>
 * 经销商撤回结果MQ消息接收处理类
 * @author ma_xueyu
 * @version 1.0
 * @createDate 2020-08-30
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class CallBackResultProcessor implements AfsMqBizProcessor<CallBackApplyResultDto> {

    private final ApplyOrderInfoService applyOrderInfoService;
    private final ApplyOprRecordService applyOprRecordService;
    private final ApplyCustBaseInfoService applyCustBaseInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(CallBackApplyResultDto entity) throws Exception {
        log.info("接收撤回申请结果："+ JSON.toJSONString(entity));
        ApplyOrderInfo orderInfo = applyOrderInfoService.getOne(Wrappers.<ApplyOrderInfo>query().lambda()
                .eq(ApplyOrderInfo::getApplyNo, entity.getApplyNo()));
        if (orderInfo != null && AfsEnumUtil.key(ResultBooleanEnum.TRUE).equals(entity.getResult())) {
            //审批人员同意： 流程外更新案件基本信息表的状态为草稿
            if(!ApplyConstants.APPLY_STATUS_PENDING.equals(orderInfo.getApplyStatus())
                &&!ApplyConstants.APPLY_STATUS_APPROVE.equals(orderInfo.getApplyStatus())){
                orderInfo.setApplyStatus(ApplyConstants.APPLY_STATUS_DRAFT);
                if(AfsEnumUtil.key(BusinessStateInEnum.SUBMITTED).equals(entity.getBusinessStateIn())){
                    //更新客户申请主表玄武查询标记
                    applyCustBaseInfoService.update(Wrappers.<ApplyCustBaseInfo>lambdaUpdate()
                            .eq(ApplyCustBaseInfo::getApplyNo,entity.getApplyNo())
                            .set(ApplyCustBaseInfo::getRiskQueriedFlag, Boolean.FALSE));
                }
            }else {
                //审批人员同意，进件申请状态为“待审核”或“审核中”： 流程内更新案件基本信息表的状态为撤回待处理
                orderInfo.setApplyStatus(ApplyConstants.CALL_BACK_WAIT_SUBMIT);
                //更新客户申请主表玄武查询标记
                applyCustBaseInfoService.update(Wrappers.<ApplyCustBaseInfo>lambdaUpdate()
                        .eq(ApplyCustBaseInfo::getApplyNo,entity.getApplyNo())
                        .set(ApplyCustBaseInfo::getRiskQueriedFlag, Boolean.TRUE));
            }
            orderInfo.setUpdateTime(new Date());
            applyOrderInfoService.updateById(orderInfo);
        }else if(orderInfo != null && AfsEnumUtil.key(ResultBooleanEnum.FALSE).equals(entity.getResult())){
            ApplyOprRecord applyOprRecord = applyOprRecordService.getOne(Wrappers.<ApplyOprRecord>query()
                    .lambda().eq(StringUtil.isNotEmpty(entity.getApplyNo()), ApplyOprRecord::getApplyNo, entity.getApplyNo())
                    .eq(ApplyOprRecord::getApplyType, String.valueOf(OrderOprType.BACK)));
            applyOprRecordService.removeById(applyOprRecord.getId());
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_CALL_BACK;
    }
}
